home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_487 / pprint / source / print.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  17KB  |  685 lines

  1. /************************************************************************
  2. *                                                                                                *
  3. *        print                                                                                    *
  4. *                                                                                                *
  5. *-----------------------------------------------------------------------*
  6. *                                                                                                *
  7. *        Module containing the real printer routines.                                *
  8. *                                                                                                *
  9. ************************************************************************/
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <intuition/intuition.h>
  14. #include <libraries/dos.h>
  15. #include <libraries/dosextens.h>
  16. #include <libraries/reqbase.h>
  17. #include <devices/printer.h>
  18. #include <private/help.h>
  19. #include <private/deva.h>
  20.  
  21. #include <proto/exec.h>
  22. #include <proto/dos.h>
  23. #include <proto/intuition.h>
  24. #include <proto/graphics.h>
  25. #include <proto/reqproto.h>
  26.  
  27. #include <string.h>
  28.  
  29. #include    "settings.h"
  30.  
  31. /***************************************
  32.  
  33.                     Definitions
  34.  
  35. ***************************************/
  36.  
  37. #define    SS        ( SHORT )Sets[ ActSet ]
  38. #define    ELITE_ON                "\x1b[2w"
  39. #define    ELITE_OFF            "\x1b[1w"
  40. #define    CONDENSED_ON        "\x1b[4w"
  41. #define    CONDENSED_OFF        "\x1b[3w"
  42. #define    LETTERQ_ON            "\x1b[2\"z"
  43. #define    LETTERQ_OFF            "\x1b[1\"z"
  44. #define    PROPORTIONAL_ON    "\x1b[2p"
  45. #define    PROPORTIONAL_OFF    "\x1b[1p"
  46. #define    LINESP6_ON            "\x1b[1z"
  47. #define    LINESP6_OFF            "\x1b[0z"
  48.  
  49. #define    PINIT                    "\x1b[0m\x1b[0w\x1b[1v\x1b[1p\x1b[0q"
  50.  
  51. /***************************************
  52.  
  53.                 External References
  54.  
  55. ***************************************/
  56. /***************************************
  57.                     Functions
  58. ***************************************/
  59.  
  60. /***************************************
  61.                     Variables
  62. ***************************************/
  63.  
  64. IMPORT    struct IODRPReq              *GPrtReq;
  65. IMPORT    struct IOStdReq              *PrtReq;
  66. IMPORT    struct IOPrtCmdReq          *CmdReq;
  67.  
  68. IMPORT    struct Window                  *FirstWindow;
  69.  
  70. IMPORT    UBYTE                                LoadString[];
  71. IMPORT    struct FileInfoBlock          *FIB;
  72. IMPORT    UBYTE                                Dir[],
  73.                                                 File[],
  74.                                                 Path[];
  75. IMPORT    struct FileRequester            FReq;
  76.  
  77. IMPORT    LONG                                Memsize;
  78. IMPORT    UBYTE                              *ActFile;
  79.  
  80. IMPORT    struct Settings                Sets[ 10 ];
  81. IMPORT    SHORT                                ActSet;
  82. IMPORT    UBYTE                                ScTitle[];
  83. IMPORT    UBYTE                                File[];
  84.  
  85. /***************************************
  86.  
  87.                         Globals
  88.  
  89. ***************************************/
  90.  
  91. UBYTE                                title[ 1024 ],
  92.                                     PrtBuf[ 1024 ],
  93.                                     Req[26]    =    "Print page no.          ?";
  94. struct TRStructure            TR    =
  95. {
  96.     Req,
  97.     NULL,
  98.     NULL,
  99.     "Skip",
  100.     "Yes",
  101.     "Quit",
  102.     "Tell me ...",
  103.     0xffff,
  104.     0,0,0,0,0L,0L
  105. };
  106.  
  107. /***************************************
  108.  
  109.                     Declarations
  110.  
  111. ***************************************/
  112.  
  113. VOID                    PreInit( VOID );
  114. VOID                    PrintPageWise( VOID );
  115. VOID                    PrintAll( VOID );
  116. VOID                    Init( struct Settings );
  117. UBYTE                  *PrintLine( UBYTE *, SHORT, SHORT );
  118. UBYTE                  *PrintPage( UBYTE *, SHORT, SHORT, SHORT, SHORT, SHORT );
  119. BOOL                    AskStatus( VOID );
  120. UBYTE                  *SkipPage( UBYTE *, SHORT, SHORT, SHORT, SHORT, SHORT );
  121. static UBYTE      *SkipLine( UBYTE *, SHORT, SHORT );
  122.  
  123. /************************************************************************
  124. *                                                                                                *
  125. *        PreInit                                                                                *
  126. *                                                                                                *
  127. *-----------------------------------------------------------------------*
  128. *                                                                                                *
  129. *        Intailizes the printer to the currently chosen settigs.                *
  130. *                                                                                                *
  131. *-----------------------------------------------------------------------*
  132. *                                                                                                *
  133. *        Parameters        :                                                                    *
  134. *            none                                                                                *
  135. *                                                                                                *
  136. *        Returns            :                                                                    *
  137. *            none                                                                                *
  138. *                                                                                                *
  139. ************************************************************************/
  140.  
  141. VOID        PreInit( VOID )
  142. {
  143.     if( AskStatus() )
  144.         Init( Sets[ ActSet ] );
  145. }
  146.  
  147. /************************************************************************
  148. *                                                                                                *
  149. *        PrintPageWise                                                                        *
  150. *                                                                                                *
  151. *-----------------------------------------------------------------------*
  152. *                                                                                                *
  153. *        Prints the whole text, asking "Print", "Skip", and "Quit" for        *
  154. *        every page.                                                                            *
  155. *                                                                                                *
  156. *-----------------------------------------------------------------------*
  157. *                                                                                                *
  158. *        Parameters        :                                                                    *
  159. *            none                                                                                *
  160. *                                                                                                *
  161. *        Returns            :                                                                    *
  162. *            none                                                                                *
  163. *                                                                                                *
  164. ************************************************************************/
  165.  
  166. VOID        PrintPageWise( VOID )
  167. {
  168.     UBYTE      *p,
  169.               *pn;
  170.     SHORT        i,
  171.                 j,
  172.                 pcnt,
  173.                 res;
  174.     BOOL        go;
  175.  
  176.     /***/
  177.  
  178.     if( AskStatus() )
  179.         Init( Sets[ ActSet ] );
  180.     else
  181.         return;
  182.  
  183.     p    =    ActFile;
  184.     i    =    1;
  185.     pcnt    =    0;
  186.     go    =    TRUE;
  187.     while(( p != NULL )&&( p < ActFile + Memsize )&&( go == TRUE ))
  188.     {
  189.         pn    =    itoa( i );
  190.         while( strlen( pn ) < 5 )
  191.             pn--;
  192.  
  193.         for( j = 0; j < 5; j++ )
  194.             Req[ j + 17 ]    =    pn[j];
  195.  
  196.         res    =    TextRequest( &TR );
  197.         switch( res )
  198.         {
  199.             case    0    :    go    =    FALSE;
  200.                             break;
  201.             case    1    :    p    =    PrintPage( p, SS.PageLen, SS.Numb, i, SS.Tabsize, SS.PageWidth );
  202.                             pcnt++;
  203.                             break;
  204.             case    2    :    p    =    SkipPage( p, SS.PageLen, SS.Numb, i, SS.Tabsize, SS.PageWidth );
  205.                             break;
  206.         }
  207.         i++;
  208.     }
  209.     SimpleRequest(    "Sent %s pages", itoa( pcnt ));
  210. }
  211.  
  212. /************************************************************************
  213. *                                                                                                *
  214. *        PrintAll                                                                                *
  215. *                                                                                                *
  216. *-----------------------------------------------------------------------*
  217. *                                                                                                *
  218. *        Prints the whole text without asking.                                        *
  219. *                                                                                                *
  220. *-----------------------------------------------------------------------*
  221. *                                                                                                *
  222. *        Parameters        :                                                                    *
  223. *            none                                                                                *
  224. *                                                                                                *
  225. *        Returns            :                                                                    *
  226. *            none                                                                                *
  227. *                                                                                                *
  228. ************************************************************************/
  229.  
  230. VOID        PrintAll( VOID )
  231. {
  232.     UBYTE      *p;
  233.     SHORT        i;
  234.  
  235.     /***/
  236.  
  237.     if( AskStatus() )
  238.         Init( Sets[ ActSet ] );
  239.     else
  240.         return;
  241.  
  242.     p    =    ActFile;
  243.     i    =    1;
  244.     while(( p != NULL )&&( p < ActFile + Memsize ))
  245.     {
  246.         p    =    PrintPage( p, SS.PageLen, SS.Numb, i, SS.Tabsize, SS.PageWidth );
  247.         i++;
  248.     }
  249.     SimpleRequest(    "Sent %s pages", itoa( --i ));
  250. }
  251.  
  252. /************************************************************************
  253. *                                                                                                *
  254. *        Init                                                                                    *
  255. *                                                                                                *
  256. *-----------------------------------------------------------------------*
  257. *                                                                                                *
  258. *        Initializes the printer                                                            *
  259. *                                                                                                *
  260. *-----------------------------------------------------------------------*
  261. *                                                                                                *
  262. *        Parameters        :                                                                    *
  263. *            set            :    Settings to init                                            *
  264. *                                                                                                *
  265. *        Returns            :                                                                    *
  266. *            none                                                                                *
  267. *                                                                                                *
  268. ************************************************************************/
  269.  
  270. VOID        Init( struct Settings set )
  271. {
  272.     static struct Settings    Store;
  273.     SHORT                            i;
  274.     UBYTE                          *p,
  275.                                   *q;
  276.     BOOL                            ne    =    FALSE;
  277.  
  278.     /***/
  279.  
  280.     /*    Compare set and Store, to determine if the set is already set, and to
  281.         give the use the opportunity, to change settings on his printer.
  282.         */
  283.  
  284.     p    =    ( UBYTE * )&Store;
  285.     q    =    ( UBYTE * )&set;
  286.  
  287.     for( i = 0; i < sizeof( struct Settings ); i++ )
  288.     {
  289.         if( p[i]    != q[i] )
  290.         {
  291.             ne    =    TRUE;
  292.             break;
  293.         }
  294.     }
  295.     if( ne == FALSE )
  296.         return;
  297.  
  298.     Printer_WRITE( PrtReq, PINIT, strlen( PINIT ));
  299.  
  300.     Printer_PRTCOMMAND( CmdReq, aSLRM, ( UBYTE )set.LMarg, ( UBYTE )set.RMarg, 0, 0 );
  301.  
  302.     if( set.Cond )
  303.         Printer_WRITE( PrtReq, CONDENSED_ON, 4L );
  304.     else
  305.         Printer_WRITE( PrtReq, CONDENSED_OFF, 4L );
  306.  
  307.     if( set.LetterQ )
  308.         Printer_WRITE( PrtReq, LETTERQ_ON, 5L );
  309.     else
  310.         Printer_WRITE( PrtReq, LETTERQ_OFF, 5L );
  311.  
  312.     if( set.LSpace6 )
  313.         Printer_WRITE( PrtReq, LINESP6_ON, 4L );
  314.     else
  315.         Printer_WRITE( PrtReq, LINESP6_OFF, 4L );
  316.  
  317.     switch( set.Style )
  318.     {
  319.         case    STYLE_ELITE    :    Printer_WRITE( PrtReq, ELITE_ON, 4L );
  320.                                     break;
  321.         case    STYLE_PICA    :    Printer_WRITE( PrtReq, ELITE_OFF, 4L );
  322.                                     break;
  323.         case    STYLE_PROP    :    Printer_WRITE( PrtReq, PROPORTIONAL_ON, 4L );
  324.                                     break;
  325.         default                :    break;
  326.     }
  327.     Store    =    set;
  328. }
  329.  
  330. /************************************************************************
  331. *                                                                                                *
  332. *        PrintLine                                                                            *
  333. *                                                                                                *
  334. *-----------------------------------------------------------------------*
  335. *                                                                                                *
  336. *        Print one line.                                                                    *
  337. *                                                                                                *
  338. *-----------------------------------------------------------------------*
  339. *                                                                                                *
  340. *        Parameters        :                                                                    *
  341. *            p                :    Pointer to the beginning of the line                *
  342. *            TS                :    Tab - size for this line.                                *
  343. *            ll                :    Line length                                                    *
  344. *                                                                                                *
  345. *        Returns            :                                                                    *
  346. *            Pointer to the beginnig of the next line, or NULL if the end    *
  347. *            of the text was encountered.                                                *
  348. *                                                                                                *
  349. ************************************************************************/
  350.  
  351. UBYTE      *PrintLine( UBYTE *p, SHORT TS, SHORT ll )
  352. {
  353.     SHORT            i,
  354.                     j,
  355.                     len,
  356.                     nt;
  357.     UBYTE          *q,
  358.                   *re;
  359.  
  360.     /***/
  361.  
  362.     if( TS == 0 )            /* Don't wanna run into a division by zero */
  363.         TS    =    1;
  364.  
  365.     /*    First, we find our return value.
  366.         */
  367.  
  368.     for( q = p;( *q != '\n' )&&( q < ( ActFile + Memsize )); q++ )
  369.         ;
  370.     if( q < ( ActFile + Memsize ))
  371.     {
  372.         q++;
  373.         re    =    q;
  374.     }
  375.     else
  376.         re    =    NULL;
  377.  
  378.     /*    Now, we process the line, using the tab replacing algorithm, we
  379.         developed for "PS". We stop, when we have written the line length.
  380.         */
  381.  
  382.     i        =    0;
  383.     len    =    0;
  384.     while(( p != q )&&( len < ll ))
  385.     {
  386.         if( *p == '\t' )
  387.         {
  388.             nt    =    TS - ( len % TS );
  389.             for( j = 0; j < nt; j++ )
  390.             {
  391.                 PrtBuf[i++]    =    ' ';
  392.                 len++;
  393.             }
  394.             p++;
  395.             continue;
  396.         }
  397.         if( *p == '\x1b' )
  398.         {
  399.             while( !isalpha( *p ))
  400.                 PrtBuf[i++]    =    *p++;
  401.  
  402.             PrtBuf[i++]    =    *p++;
  403.             continue;
  404.         }
  405.         PrtBuf[i++]    =    *p++;
  406.         len++;
  407.         continue;
  408.     }
  409.     PrtBuf[ i - 1 ]    =    '\n';
  410.     Printer_WRITE( PrtReq, PrtBuf, ( LONG )i );
  411.     if( p < q )
  412.         re    =    p;
  413.  
  414.     return    re;
  415. }
  416.  
  417. /************************************************************************
  418. *                                                                                                *
  419. *        PrintPage                                                                            *
  420. *                                                                                                *
  421. *-----------------------------------------------------------------------*
  422. *                                                                                                *
  423. *        Print one page of text, including the concluding form feed.            *
  424. *                                                                                                *
  425. *-----------------------------------------------------------------------*
  426. *                                                                                                *
  427. *        Parameters        :                                                                    *
  428. *            p                :    Pointer to the beginning of the page                *
  429. *            pl                :    Page length    ( in lines )                                *
  430. *            numb            :    Page numbering flag ( 0 = No, 1 = Top, 2 = Bot,    *
  431. *                                3 = Line )                                                    *
  432. *            nr                :    Page number                                                    *
  433. *            TS                :    Tab size                                                        *
  434. *            ll                :    Line length                                                    *
  435. *                                                                                                *
  436. *        Returns            :                                                                    *
  437. *            Pointer to the beginning of the next page, of NULL, if we        *
  438. *            reached the end of the text.                                                *
  439. *                                                                                                *
  440. ************************************************************************/
  441.  
  442. UBYTE      *PrintPage( UBYTE *p, SHORT pl, SHORT numb, SHORT nr, SHORT TS, SHORT ll )
  443. {
  444.     UBYTE          *q,
  445.                   *ns,
  446.                     lf[]    =    "\n",
  447.                     ff[]    =    "\xc";
  448.     SHORT            i,
  449.                     j;
  450.  
  451.     /***/
  452.  
  453.     pl--;
  454.  
  455.     if( numb != 0 )
  456.     {
  457.         for( i = 0; i < ll; i++ )
  458.             title[i]    =    ' ';
  459.         pl    -=    2;
  460.     }
  461.     if( numb == 3 )
  462.     {
  463.         q    =    stpcpy( title, File );
  464.         *q    =    ' ';
  465.     }
  466.     if( numb != 0 )
  467.     {
  468.         q    =    stpcpy( &title[ ll - 12 ], "Page:" );
  469.         *q++    =    ' ';
  470.         ns    =    itoa( nr );
  471.         while( strlen( ns ) < 5 )
  472.             ns--;
  473.  
  474.         q    =    stpcpy( q, ns );
  475.         *q    =    '\n';
  476.     }
  477.  
  478.     if(( numb == 1 )||( numb == 3 ))
  479.     {
  480.         Printer_WRITE( PrtReq, title, ( LONG )ll );
  481.         Printer_WRITE( PrtReq, lf, 1L );
  482.     }
  483.  
  484.     for( i = 0; i < pl; i++ )
  485.     {
  486.         p    =    PrintLine( p, TS, ll );
  487.         if( p == NULL )
  488.         {
  489.             for( j = i; j < pl; j++ )
  490.                 Printer_WRITE( PrtReq, lf, 1L );
  491.  
  492.             break;
  493.         }
  494.     }
  495.  
  496.     if( numb    ==    2 )
  497.     {
  498.         Printer_WRITE( PrtReq, lf, 1L );
  499.         Printer_WRITE( PrtReq, title, ( LONG )ll );
  500.     }
  501.     Printer_WRITE( PrtReq, ff, 1L );
  502.  
  503.     return    p;
  504. }
  505.  
  506. /************************************************************************
  507. *                                                                                                *
  508. *        AskStatus                                                                            *
  509. *                                                                                                *
  510. *-----------------------------------------------------------------------*
  511. *                                                                                                *
  512. *        Asks the printer status.                                                        *
  513. *                                                                                                *
  514. *-----------------------------------------------------------------------*
  515. *                                                                                                *
  516. *        Parameters        :                                                                    *
  517. *            none                                                                                *
  518. *                                                                                                *
  519. *        Returns            :                                                                    *
  520. *            TRUE if the printer is ready to print, FALSE if the printer        *
  521. *            is not ready.                                                                    *
  522. *                                                                                                *
  523. ************************************************************************/
  524.  
  525. BOOL        AskStatus( VOID )
  526. {
  527.     UWORD        status;
  528.  
  529.     /***/
  530.  
  531.     Printer_QUERY( PrtReq, &status );
  532.     /*    SimpleRequest( "status is %s", itoa( status )); */
  533.     switch( status & 0x0700 )
  534.     {
  535.         case    0x0400    :    return    TRUE;
  536.                                 break;
  537.         case    0x0500    :    SimpleRequest( "Please turn your printer online !" );
  538.                                 return    FALSE;
  539.                                 break;
  540.         case    0x0700    :    SimpleRequest( "Please insert paper and\nturn your printer online !" );
  541.                                 return    FALSE;
  542.                                 break;
  543.         default            :    SimpleRequest( "Something is wrong with your printer !" );
  544.                                 return    FALSE;
  545.                                 break;
  546.     }
  547. }
  548.  
  549. /************************************************************************
  550. *                                                                                                *
  551. *        SkipPage                                                                                *
  552. *                                                                                                *
  553. *-----------------------------------------------------------------------*
  554. *                                                                                                *
  555. *        Skip one page of text.                                                            *
  556. *                                                                                                *
  557. *-----------------------------------------------------------------------*
  558. *                                                                                                *
  559. *        Parameters        :                                                                    *
  560. *            p                :    Pointer to the beginning of the page                *
  561. *            pl                :    Page length    ( in lines )                                *
  562. *            numb            :    Page numbering flag ( 0 = No, 1 = Top, 2 = Bot,    *
  563. *                                3 = Line )                                                    *
  564. *            nr                :    Page number                                                    *
  565. *            TS                :    Tab size                                                        *
  566. *            ll                :    Line length                                                    *
  567. *                                                                                                *
  568. *        Returns            :                                                                    *
  569. *            Pointer to the beginning of the next page, of NULL, if we        *
  570. *            reached the end of the text.                                                *
  571. *                                                                                                *
  572. ************************************************************************/
  573.  
  574. UBYTE      *SkipPage( UBYTE *p, SHORT pl, SHORT numb, SHORT nr, SHORT TS, SHORT ll )
  575. {
  576.     SHORT            i,
  577.                     j;
  578.  
  579.     /***/
  580.  
  581.     pl--;
  582.  
  583.     if( numb != 0 )
  584.     {
  585.         pl    -=    2;
  586.     }
  587.  
  588.     for( i = 0; i < pl; i++ )
  589.     {
  590.         p    =    SkipLine( p, TS, ll );
  591.         if( p == NULL )
  592.         {
  593.             for( j = i; j < pl; j++ )
  594.                 ;
  595.             break;
  596.         }
  597.     }
  598.     return    p;
  599. }
  600. /************************************************************************
  601. *                                                                                                *
  602. *        SkipLine                                                                                *
  603. *                                                                                                *
  604. *-----------------------------------------------------------------------*
  605. *                                                                                                *
  606. *        Skip one line.                                                                        *
  607. *                                                                                                *
  608. *-----------------------------------------------------------------------*
  609. *                                                                                                *
  610. *        Parameters        :                                                                    *
  611. *            p                :    Pointer to the beginning of the line                *
  612. *            TS                :    Tab - size for this line.                                *
  613. *            ll                :    Line length                                                    *
  614. *                                                                                                *
  615. *        Returns            :                                                                    *
  616. *            Pointer to the beginnig of the next line, or NULL if the end    *
  617. *            of the text was encountered.                                                *
  618. *                                                                                                *
  619. ************************************************************************/
  620.  
  621. static UBYTE      *SkipLine( UBYTE *p, SHORT TS, SHORT ll )
  622. {
  623.     SHORT            i,
  624.                     j,
  625.                     len,
  626.                     nt;
  627.     UBYTE          *q,
  628.                   *re;
  629.  
  630.     /***/
  631.  
  632.     if( TS == 0 )            /* Don't wanna run into a division by zero */
  633.         TS    =    1;
  634.  
  635.     /*    First, we find our return value.
  636.         */
  637.  
  638.     for( q = p;( *q != '\n' )&&( q < ( ActFile + Memsize )); q++ )
  639.         ;
  640.     if( q < ( ActFile + Memsize ))
  641.     {
  642.         q++;
  643.         re    =    q;
  644.     }
  645.     else
  646.         re    =    NULL;
  647.  
  648.     /*    Now, we process the line, using the tab replacing algorithm, we
  649.         developed for "PS". We stop, when we have written the line length.
  650.         */
  651.  
  652.     i        =    0;
  653.     len    =    0;
  654.     while(( p != q )&&( len < ll ))
  655.     {
  656.         if( *p == '\t' )
  657.         {
  658.             nt    =    TS - ( len % TS );
  659.             for( j = 0; j < nt; j++ )
  660.             {
  661.                 PrtBuf[i++]    =    ' ';
  662.                 len++;
  663.             }
  664.             p++;
  665.             continue;
  666.         }
  667.         if( *p == '\x1b' )
  668.         {
  669.             while( !isalpha( *p ))
  670.                 PrtBuf[i++]    =    *p++;
  671.  
  672.             PrtBuf[i++]    =    *p++;
  673.             continue;
  674.         }
  675.         PrtBuf[i++]    =    *p++;
  676.         len++;
  677.         continue;
  678.     }
  679.     PrtBuf[ i - 1 ]    =    '\n';
  680.     if( p < q )
  681.         re    =    p;
  682.  
  683.     return    re;
  684. }
  685.